#include "MCU_include.h"


void Voletage_PowerLimit( )
{			
	if((ADC_V_result >= c_AD_180V && ADC_V_result <c_AD_200V) || (ADC_V_result >= c_AD_240V  && ADC_V_result < c_AD_270V ))	
	{													
		if(u16_PowerSet > 2800)				//2800W
		{
			u16_PowerSetBack0 = u16_PowerSet;
			u16_PowerSet  = 2800;
			Fg_PowLimit30 = 1;
		}
	}
	else if(Fg_PowLimit30)
	{
		Fg_PowLimit30 = 0;
		u16_PowerSet  = u16_PowerSetBack0;
	}								
			
	if((ADC_V_result >= c_AD_160V) && (ADC_V_result < c_AD_180V))	
	{
		if(u16_PowerSet > 2000)
		{
			u16_PowerSetBack1=u16_PowerSet;
			u16_PowerSet  = 2000;
			Fg_PowLimit25 = 1;
		}
	}
	else if(Fg_PowLimit25)
	{
		Fg_PowLimit25 = 0;
		u16_PowerSet  = u16_PowerSetBack1;
	}

	if (FLAG.flagBit.PotType)
	{
		if (u16_PowerSet > MAX_POWER_304)
		u16_PowerSet  = MAX_POWER_304;
	}
	else
	{
		if (u16_PowerSet > MAX_POWER_IRON)
		u16_PowerSet  = MAX_POWER_IRON;
	}
			
}



void VoltageTemperatureDet()
{
	static u8	OverVacCnt=0, UnderVacCnt=0;
	static u8 	IGBT_OTP_LimitPowerCnt=0, IGBT_OTP_PowerOffCnt=0, IGBT_OTP_LimitPower_RecCnt=0;
	static u8 	T_PAN_Pro_Cnt=0/*,T_PAN_Rec_Cnt=0*/;
	
	//-----------------------------------OVP------------------------------------//
	if(ADC_V_result> MAX_VAC)	
	{
		if(OverVacCnt < OverVacCntTH)
			OverVacCnt++;
		else
		{
			IH_Status_APP.flagBit.error   = ERROR_OVP;		//Overvoltage fault
			ADC_FLAG.flagBit.Over_Vac = 1;
			protectCODE_u16 = protectCODE_u16 | 0b00000001 ;
			
		}
	}
	else if(OverVacCnt > 0)	OverVacCnt = 0;
    
    //-----------------------------------UVP------------------------------------//     				
	if(ADC_V_result < MIN_VAC)		
	{
		if(UnderVacCnt < UnderVacCntTH)
			UnderVacCnt++;
		else
 		{	         				
			IH_Status_APP.flagBit.error = ERROR_UVP;			//Undervoltage fault
			ADC_FLAG.flagBit.Under_Vac = 1;
				protectCODE_u16 = protectCODE_u16 | 0b00000010 ;
		}
	}
	else if(UnderVacCnt > 0)	UnderVacCnt--;
         				
	if(ADC_FLAG.flagBit.Under_Vac)
	{
		if(ADC_V_result >= Rec_V)
			ADC_FLAG.flagBit.Rec_Vac = 1;
		else if(ADC_V_result < Rec_V)
			IH_Status_APP.flagBit.error = ERROR_UVP;
	}
    
    //-----------------------------------IGBT OTP------------------------------------//     				
	if(AD_Temp_IGBT <= T_IGBT_80)	
	{
		if(IGBT_OTP_LimitPowerCnt < T_IGBT_LimitPower_CntTH)	
			IGBT_OTP_LimitPowerCnt++;
		else	
			ADC_FLAG.flagBit.IGBT_OTP_LimitPower_F = 1;
         					
		if(ADC_FLAG.flagBit.IGBT_OTP_PowerOff_F == 1)	IH_Status_APP.flagBit.error = ERROR_IGBT_OVER_TMP;
		if(AD_Temp_IGBT <= T_IGBT_100)	
		{
			if(IGBT_OTP_PowerOffCnt < T_IGBT_PowerOff_CntTH)
				IGBT_OTP_PowerOffCnt++;
			else
			{
				ADC_FLAG.flagBit.IGBT_OTP_PowerOff_F = 1;
				IH_Status_APP.flagBit.error = ERROR_IGBT_OVER_TMP;		//IGBT over temperature state
					protectCODE_u16 = protectCODE_u16 | 0b00000100 ;
			}
		}
		else if(IGBT_OTP_PowerOffCnt>0)	IGBT_OTP_PowerOffCnt--;
	}
	else 
	{
		if(IGBT_OTP_LimitPowerCnt > 0)	IGBT_OTP_LimitPowerCnt--;
		if(IGBT_OTP_PowerOffCnt > 0)	IGBT_OTP_PowerOffCnt--;
	}
	
	//-----------------------------------IGBT OTP Limit Power Receve------------------------------------//
	if(ADC_FLAG.flagBit.IGBT_OTP_LimitPower_F)
	{
		if(AD_Temp_IGBT >= T_IGBT_LimitPower_RecTH)
		{
			if(IGBT_OTP_LimitPower_RecCnt < 5)	
				IGBT_OTP_LimitPower_RecCnt++;
			else	
				ADC_FLAG.flagBit.IGBT_OTP_LimitPower_F = 0;
		}
		else if(IGBT_OTP_LimitPower_RecCnt > 0)
		{
			IGBT_OTP_LimitPower_RecCnt--;
		}
	}

	//-----------------------------------PAN OTP Limit Power Receve------------------------------------//					
	if(AD_Temp_PAN <= T_PAN_150)						
	{
		if(T_PAN_Pro_Cnt < T_PAN_PRO_CntTH)	
			T_PAN_Pro_Cnt++;
		else	
		{
			ADC_FLAG1.flagBit.PAN_PRO_F = 1;
			IH_Status_APP.flagBit.error = ERROR_PAN_OVER_TMP;		//Overheating state of furnace surface
				protectCODE_u16 = protectCODE_u16 | 0b00001000 ;
		}
	         				
	}
	else if (T_PAN_Pro_Cnt > 0) T_PAN_Pro_Cnt--;			

//	if(AD_Temp_PAN < T_PAN_150)					
//	{
//		if(T_PAN_Rec_Cnt < T_PAN_REC_CntTH)		
//			T_PAN_Rec_Cnt++;				
//		else	
//		{
//			if (ADC_FLAG1.flagBit.PAN_PRO_F)
//			{
	//			ADC_FLAG1.flagBit.PAN_PRO_F = 0;
//				if (IH_Status_APP.flagBit.error == ERROR_PAN_OVER_TMP)
//				IH_Status_APP.flagBit.error = NO_ERROR_CODE;	
//				protectCODE_u16 = protectCODE_u16 | 0b00010000 ;
//			}
//		}
//	         				
//	}
//	else if (T_PAN_Rec_Cnt > 0) T_PAN_Rec_Cnt--;	
}

//******************** ****************//
//
//
//
//******************************************************//

void Surge_Protect()	// 
{
	if(	ErrRecoveryTimer > 0 )
	{
		ErrRecoveryTimer--;
		if( ErrRecoveryTimer == 0)		
		{
			 PWM_F	=PWM_F_Back ;
			_mss0 = 0;
			_mss1 = 0;
			_fpwmclf =0;
			FG_IH_Status.flagBit.PWM_ON_OFF = 1;	
			PWM_Update(PWM_F);
			PWM_ON();
			bSurge_error = 0;
		
			IH_Status_APP.flagBit.error = NO_ERROR_CODE;
			
		}
	}

}
//******************** ****************//
//
//
//
//******************************************************//
void Error_Processing()
{
	switch(IH_Status_APP.flagBit.error)
	{
		
		case ERROR_IGBT_NTC:		
		case ERROR_PAN_NTC:		
				if(FG_IH_Status.flagBit.PWM_ON_OFF == 1)
				{
					u16_PowerSetBack0 = u16_PowerSet;
					IGBT_OFF(PWM_F);
					FG_IH_Status.flagBit.PWM_ON_OFF = 0;
				}
				break;			
		
		case ERROR_UVP:		
		case ERROR_OVP:		
		case ERROR_IGBT_OVER_TMP:	
		case ERROR_PAN_OVER_TMP:		
				if(FG_IH_Status.flagBit.PWM_ON_OFF == 1)
				{
					if(ADC_FLAG.flagBit.IGBT_OTP_PowerOff_F)				
					{
						if(u16_PowerSetBack0 < 2000)
						{
							u16_PowerSetBack0 = u16_PowerSet;
						}
					}
					else	
					u16_PowerSetBack0 = u16_PowerSet;
					IGBT_OFF(PWM_F);
					PWM_F = d_PWM_F;					
					FG_IH_Status.flagBit.PWM_ON_OFF = 0;
				}
				
				break;		
				
		case 0:
//				if(ADC_FLAG.flagBit.IGBT_OTP_LimitPower_F == 1)
//				{
//					if(ADC_FLAG.flagBit.IGBT_OTP_PowerOff_F == 0)
//					{
//						if(u16_PowerSet > 2200)	
//						{
//							u16_PowerSetBack0 = u16_PowerSet;
//							u16_PowerSet = 2200;
//						}
//						
//					}
//				}
//				else
				if(ADC_FLAG.flagBit.IGBT_OTP_LimitPower_F == 0)
				{
					if(ControlSet_App.flagBit.on_off==1 && FG_IH_Status.flagBit.PWM_ON_OFF==0 &&u16_PowerSet !=0)
					{
						if(ADC_FLAG.flagBit.Over_Vac || (ADC_FLAG.flagBit.Under_Vac && ADC_FLAG.flagBit.Rec_Vac) ||
						 	ADC_FLAG.flagBit.IGBT_OTP_PowerOff_F)
						{
							_ovp2f = 0;
							_ovp4f = 0;
							_msd1 = 0; _msd0 = 0;
							PWM_ON();
							_msd1 = 0; _msd0 = 0;
							if(ADC_FLAG.flagBit.Over_Vac)	ADC_FLAG.flagBit.Over_Vac = 0;
							if(ADC_FLAG.flagBit.Under_Vac && ADC_FLAG.flagBit.Rec_Vac)	
							{
								ADC_FLAG.flagBit.Under_Vac = 0;
								ADC_FLAG.flagBit.Rec_Vac = 0;
							}
							if(ADC_FLAG.flagBit.IGBT_OTP_PowerOff_F)	ADC_FLAG.flagBit.IGBT_OTP_PowerOff_F = 0;
							FG_IH_Status.flagBit.PWM_ON_OFF = 1;
							u16_PowerSet = u16_PowerSetBack0;
						}
					}
				}
				break;
		
	}
}

void IGBT_OTP_LimitPower()
{
	if(ADC_FLAG.flagBit.IGBT_OTP_LimitPower_F)
	{
		if(u16_PowerSet > 3000)	
		{
			u16_PowerSet = 3000;
		}			
	}
}